package de.tudarmstadt.gdi1.project.cipher.substitution.monoalphabetic;

import java.util.ArrayList;
import java.util.List;

import javax.swing.text.Utilities;

import de.tudarmstadt.gdi1.project.Charset;
import de.tudarmstadt.gdi1.project.alphabet.Alphabet;

public class KeywordMonoalphabetic_IMP extends MonoalphabeticCipher_IMP implements KeywordMonoalphabeticCipher  {

	
		public KeywordMonoalphabetic_IMP(String key, Alphabet alphabet)		{
					super(alphabet,getAlphabetByKeyword(key,alphabet));
			
			
			
			
		}
	
	
	public static Alphabet getAlphabetByKeyword(String key, Alphabet alphabet){
		
		
		char[] alph = new char [alphabet.size()];
		char[] akey  = key.toCharArray();
		int n = 0; 
		
		for(int i=0; i < key.length();i++) {
			
			if(!contains(akey[i],alph)) {
				
				alph[n] = akey[i];
				n++;
				
			}
			
			
			
		}
		
		
		List<Character> reddition = new ArrayList<Character>();
		char [] realph = (new Utils_IMP()).reverseAlphabet.Charset.asChararray();
			for(int i = 0; i <alphabet.size();i++) {
				
				if (!contains(realph[i], alph)) {
					
					alph[n] = realph[i];
						n++;
				}
			}
	
				for (char c : alph) {
					reddition.add(c);
				}
	
				return new Charset(reddition);
				
	}
	
	
			private static boolean contains (char chr, char[] array) {
				
				for(char c : array){
					
					if(c==chr){
						return true;
					}
					
				}
				return false;
			}
	
	
	
	
}
